
dat <- read.csv("AfricanAutocratsExits.csv")

years <- seq(from=min(dat$year), to=max(dat$year), by=1)
n.years <- n.cex <- n.assassinations <- n.civilwars <- n.consensus <- n.coup <- n.elections <- n.foreign <- n.interim <- n.natural <- n.nocontest <- n.resignation <- n.revolt <- n.stepdown <- n.termlimit <- n.other <- array(dim=(length(years)), data=0)
for(i in 1:length(years)){
	store <- dat[dat$year==years[i],]
	n.years[i] <- nrow(store)
	n.cex[i] <- sum(na.omit(store$cex))
	for(j in 1:nrow(store)){
		ifelse(store$exit[j]=="assassination", n.assassinations[i]<-n.assassinations[i]+1, n.assassinations[i]<-n.assassinations[i])
		ifelse(store$exit[j]=="civil war", n.civilwars[i]<-n.civilwars[i]+1, n.civilwars[i]<-n.civilwars[i])
		ifelse(store$exit[j]=="consensus", n.consensus[i]<-n.consensus[i]+1, n.consensus[i]<-n.consensus[i])
		ifelse(store$exit[j]=="coup", n.coup[i]<-n.coup[i]+1, n.coup[i]<-n.coup[i])
		ifelse(store$exit[j]=="elections", n.elections[i]<-n.elections[i]+1, n.elections[i]<-n.elections[i])
		ifelse(store$exit[j]=="foreign", n.foreign[i]<-n.foreign[i]+1, n.foreign[i]<-n.foreign[i])
		ifelse(store$exit[j]=="interim", n.interim[i]<-n.interim[i]+1, n.interim[i]<-n.interim[i])
		ifelse(store$exit[j]=="natural", n.natural[i]<-n.natural[i]+1, n.natural[i]<-n.natural[i])
		ifelse(store$exit[j]=="no contest", n.nocontest[i]<-n.nocontest[i]+1, n.nocontest[i]<-n.nocontest[i])
		ifelse(store$exit[j]=="resignation", n.resignation[i]<-n.resignation[i]+1, n.resignation[i]<-n.resignation[i])
		ifelse(store$exit[j]=="revolt", n.revolt[i]<-n.revolt[i]+1, n.revolt[i]<-n.revolt[i])
		ifelse(store$exit[j]=="stepdown", n.stepdown[i]<-n.stepdown[i]+1, n.stepdown[i]<-n.stepdown[i])
		ifelse(store$exit[j]=="term limit", n.termlimit[i]<-n.termlimit[i]+1, n.termlimit[i]<-n.termlimit[i])
		ifelse(store$exit[j]=="other", n.other[i]<-n.other[i]+1, n.other[i]<-n.other[i])
	}
}

counts <- t(cbind(n.natural, n.coup+n.assassinations, n.years-n.interim-n.assassinations-n.coup-n.foreign-n.natural))
colnames(counts) <- years

par(mar=c(bottom=4, left=3, top=1, right=0), bg=NA)
barplot(height=counts, space=0, beside=FALSE, axes=F, border="black", col=c("black", "lightgrey", "white"), bg=NA, col.axis="White", ylim=c(0,8))
axis(side=1, at=c(3,8,13,18,23,28,33,38,43), labels=c("1965", "1970", "1975", "1980", "1985", "1990", "1995", "2000", "2005"), las=1, tick=T, cex.axis=1, col="Black", col.ticks="Black", col.axis="Black")
mtext("Year", side=1, line=3, cex=1, col="Black")
axis(side=2, at=c(0,2,4,6,8), las=1, tick=T, cex.axis=1, col="Black", col.ticks="Black", col.axis="Black")
mtext("Number of Exits", side=2, line=2, cex=1, col="Black")
legend(x=0.0, y=8.0, legend=c("Natural Death", "Coups and Assassinations", "Revolts, Elections, Term Limits, and Resignations"), fill=c("black", "lightgrey", "white"), cex=1.0, box.col=NA, bg=NA, text.col="Black")
abline(v=27, col="Black", lwd=3)
dev.print(file="AfricanAutocratsExits.pdf",device=pdf)

